package com.item.demo.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Calendar;
import java.util.UUID;

public class UploadFileUtils {

    /**
     * 生成文件名+后缀
     *
     * @return
     */
    public static String CreateFileUrl(String path) {
        //获取.的位置
        int index = path.lastIndexOf(".");
        //获取后缀
        String extions = path.substring(index);
        //返回随机生成+后缀
        return UUID.randomUUID() + extions;
    }

    /**
     * 生成当前日期字符串
     *
     * @return
     */
    public static String CreateNowDate() {
        Calendar now = Calendar.getInstance();
        String nowDate = "";
        nowDate += now.get(Calendar.YEAR);
        nowDate += (now.get(Calendar.MONTH) + 1);
        nowDate += now.get(Calendar.DAY_OF_MONTH);
        return nowDate;
    }

    /**
     * 判断是否是图片
     *
     * @param file
     * @return
     */
    public static boolean isImage(File file) {
        if (!file.exists()) {
            return false;
        }
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
            if (image == null || image.getWidth() <= 0 || image.getHeight() <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}